package com.example.demo.designMode.Proxy;

import java.lang.reflect.*;

/**
* @description: TODO 动态代理
* @author 杨镇宇
* @date 2022/3/2 16:27
* @version 1.0
*/

public class DyProxy {

    interface Person{
        void applyJob();
    }

    static class Boss implements Person {
        @Override
        public void applyJob() {
            System.out.println("部门领导面试通过后帮忙安排工作，推荐给公司大领导");
        }
    }

    static class Hr implements InvocationHandler {

        private Object target;
        public Hr(Object target){
            this.target = target;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //这里写具体的业务逻辑
            System.out.println("hr 检查应聘者是否符合工作要求，推荐给部门领导");
            //通过反射来实现
            Object o = method.invoke(target,args);
            return o;
        }
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Boss boss = new Boss();
        Class <?> proxyclazz = Proxy.getProxyClass(Person.class.getClassLoader(),Person.class);
        Constructor<?> constructor = proxyclazz.getConstructor(InvocationHandler.class);
        Person person = (Person)constructor.newInstance(new Hr(boss));
        person.applyJob();
    }
}
